<!-- 通用佣金计划信息 -->
<template>
  <div class="table">
    <el-tabs v-model="activeName" @tab-click="handleChangeTabs">
      <el-tab-pane label="通用佣金计划列表" name="one">
        <SearchTable :contentType="contentType" :cpsStatus="cpsStatus" ref="searchTableRef" v-model:formStatus="formStatus" @handleAdd="handleAdd" @handleViewEdit="handleViewEdit" />
      </el-tab-pane>
      <el-tab-pane label="通用佣金计划详情" name="two">
        <FormTab :contentType="contentType" v-model:formStatus="formStatus" v-model:form="form" @handleInitData="clearData" @handleSuccess="handleSuccess" />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script setup lang="ts" name="commissionPlanInfo">
import SearchTable from "@/components/douyinManager/commissionPlanInfo/SearchTable.vue";
import FormTab from "@/components/douyinManager/commissionPlanInfo/FormTab.vue";
import type { TabsMode, ViewMode, ViewModeFilter } from "@/types/common";
import type { TabsPaneContext } from "element-plus";
import type { ICommissionPlanInfo, ICommissionPlanInfoDTO } from "@/types/douyinManager/commissionPlanInfo";
import { useDictData } from "@/hooks/network/useDictData";
import type { IDictData } from "@/types/dict";
let { getDictData } = useDictData();
/* 抖音通用计划状态 */
const cpsStatus = ref<Array<IDictData>>([]);
/* 抖音计划带货场景 */
const contentType = ref<Array<IDictData>>([]);
/* 表格状态 */
const formStatus = ref<ViewMode>("none");
/* 标签页切换 */
const activeName = ref<TabsMode>("one");
/* 表格子组件的引用 */
const searchTableRef = useTemplateRef("searchTableRef");

const form = reactive<ICommissionPlanInfoDTO>({
  id: 0,
  goods_id: 0,
  goods_name: "",
  commission_rate: "",
  content_type: ""
});

/* 处理切换标签页 */
function handleChangeTabs(tab: TabsPaneContext) {
  if (tab.props.name === "two") {
    formStatus.value = "edit";
  } else if (tab.props.name === "one") {
    clearData();
  }
}

/* 清除数据 */
function clearData() {
  activeName.value = "one";
  formStatus.value = "none";
}

/* 处理修改成功 */
function handleSuccess() {
  searchTableRef.value?.getTableInfo();
}

/* 点击添加按钮 */
function handleAdd(code: ViewMode) {
  formStatus.value = code;
  activeName.value = "two";
}
/* 点击编辑按钮 */
function handleViewEdit(row: ICommissionPlanInfo, code: ViewModeFilter) {
  formStatus.value = code;
  activeName.value = "two";
  /* nextTick 解决重置该表单项 初始化的数据问题 */
  nextTick(() => {
    form.id = row.id;
    form.goods_id = row.goods_id;
    form.goods_name = row.goods.goods_name;
    form.commission_rate = row.commission_rate;
    form.content_type = row.content_type + "";
  });
}

getDictData("DOUYIN_COMMON_CPS_STATUS", cpsStatus);
getDictData("DOUYIN_CPS_CONTENT_TYPE", contentType);
</script>

<style lang="scss" scoped></style>
